`<?php

require_once './vendor/autoload.php';
//require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;

$connection = new AMQPStreamConnection('172.17.0.3', 5672, 'admin', 'admin','msg');
$channel = $connection->channel();

$channel->exchange_declare('toalluser', 'fanout', false, false, false);

list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);

$channel->queue_bind($queue_name, 'toalluser');

echo " [*] Waiting for logs. To exit press CTRL+C\n";

require "src/PHPMailer.php";
require "src/SMTP.php";

$callback = function ($msg) {
    echo ' [x] ', $msg->body, "\n";

    $arr = $msg->body;
    $data = json_decode($arr,true);
    $address = $data['email'];

    $mail = new \PHPMailer\PHPMailer\PHPMailer();
    $mail->isSMTP();
    $mail->isHTML(TRUE);
    $mail->CharSet = 'UTF-8';
    $mail->SMTPAuth = TRUE;
    $mail->From = '15210602184@163.com';
    $mail->FromName = 'zty';
    $mail->Host = "ssl://smtp.163.com";
    $mail->Username = "15210602184@163.com";       //用户名
    $mail->Password = "cq2757856";       //密码
    $mail->Port = 465;       //发邮件端口号默认25
    $mail->AddAddress($address);     //收件人
    $mail->Subject = "邮件发送";    //标题
    $mail->Body = "今天学习邮件发送";     //内容
    $res = $mail->send();
    if ($res){
        echo 1;
    }else{
        echo 0;
    }
};


$channel->basic_consume($queue_name, '', false, true, false, false, $callback);

while (count($channel->callbacks)) {
    $channel->wait();
}

$channel->close();
$connection->close();
